package com.securitytrace.client.rfid.driver.console;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import com.securitytrace.client.rfid.driver.Address;
import com.securitytrace.client.rfid.driver.Reader;

public class ConsoleReader implements Reader {

	public String read(Address address) throws IOException {
		return readFrom((LineAddress) address);
	}

	private String readFrom(LineAddress address) throws IOException {
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(new File(address.getFile())));
			int lineNum = 1;
			String line = null;
			while((line = reader.readLine()) != null) {
				if(lineNum == address.getLine()) {
					return line;
				}
				lineNum ++;
			}
			throw new IOException("can't read line:" + address.getLine());
		} finally {
			if(reader != null) {
				reader.close();
			}
		}
	}

}
